package com.zhongkai.listener;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RocketMqProducer {

    private static final Logger LOGGER = LoggerFactory.getLogger(RocketMqProducer.class);

    public static DefaultMQProducer producer = null;

    public static void init() {
        producer = new DefaultMQProducer("myMQ");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.setInstanceName("tomcat");
        try {
            producer.start();
        } catch (MQClientException e) {

        }
    }

/**
     * rocketmq发送消息方法
     *
     * @param topic      组名
     * @param tagName    同一个topic下的不同 分支,同一个消费者只能取一个组的下的不同的tag分支
     * @param msgBody    消息体
     * @return
     * @author TF013242 2018-1-20 下午2:32:19*/


    public static void sendMsgIntime(String topic, String tagName, byte[] msgBody) {

        Message msg = new Message(topic, tagName,  msgBody);
        msg.setDelayTimeLevel(0);
        try {
            String result = producer.send(msg).toString();
            LOGGER.info("send rockmq topic:" + topic + " " + result);
        } catch (Exception e) {
            LOGGER.error("send rockmq error topic:" + topic + new String(msgBody), e);
        }
    }
}




